package org.jiucai.app.admin.auth.dao;

import java.util.List;

import org.jiucai.app.admin.AdminBaseDao;
import org.jiucai.appframework.common.encode.MD5;
import org.jiucai.cas.auth.dao.AuthDao;
import org.jiucai.cas.domain.AuthQuery;
import org.jiucai.cas.domain.UserInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

@Repository("adminAuthDao")
public class AdminAuthDaoImpl extends AdminBaseDao implements AuthDao {
	
	
	@Deprecated
	protected UserInfo getUserInfo(){
		UserInfo user = new UserInfo();
		user.setUserId(1L);
		user.setLoginName("admin");
		user.setPassword(MD5.encode("admin"));
		
		user.setDisplayName("超级管理员");
		
		return user;
	}
	
	/**
	 * 登录限制条件
	 * @return
	 */
	protected String getUserCondition(){
		StringBuffer sql = new StringBuffer();
		
		sql.append(" and u.locked = 0 ");
		sql.append(" and u.deleted = 0 ");
		sql.append(" and u.user_type = '");
		sql.append(UserInfo.USER_TYPE_ADMIN);
		sql.append("'  ");
		
		return sql.toString();
	}

	/**
	 * 检查用户是否存在
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public Boolean checkUser(AuthQuery query) {
		
		 
		
		if(null == query){
			return false;
		}
		
		return "admin".equals(query.getLoginName()) ? true : false;
//
//		StringBuffer sql = new StringBuffer();
//
//		sql.append(" select ");
//		sql.append("	count(*) as ct ");
//		sql.append("  from user_info u ");
//		sql.append(" where u.user_id = :userId ");
//		sql.append(getUserCondition());
//
//		int userCount = getQueryDao().queryForInt(sql.toString(),
//				new BeanPropertySqlParameterSource(query));
//
//		log.debug("userCount " + userCount);
//
//		return userCount > 0 ? true : false;

	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public UserInfo getUserInfo(AuthQuery query) {
		UserInfo user = null;
		
		if(null == query){
			return null;
		}

		StringBuffer sql = new StringBuffer();

		sql.append(" select ");
		sql.append("        u.user_id as user_id, ");
		sql.append("        u.login_name, ");
		sql.append("        u.display_name, ");
		sql.append("        u.password ");
		sql.append("  from user_info u ");
		sql.append(" where u.user_id = :userId ");
		sql.append(getUserCondition());

		// 定义返回值类型
		BeanPropertyRowMapper<UserInfo> mapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);

		// 定义SQL 绑定参数值
		SqlParameterSource param = new BeanPropertySqlParameterSource(query);

		List<UserInfo> userList = getQueryDao().query(sql.toString(),mapper, param);
		
		if(null != userList && userList.size() > 0 ){
			user = userList.get(0);
		}

		return user;

	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public UserInfo getUserInfo(UserInfo query) {
		
		return getUserInfo();
		
//		if(null == query){
//			return null;
//		}
//
//		StringBuffer sql = new StringBuffer();
//
//		sql.append(" select ");
//		sql.append("        u.user_id as user_id, ");
//		sql.append("        u.login_name, ");
//		sql.append("        u.display_name, ");
//		sql.append("        u.password ");
//		sql.append("  from user_info u ");
//		sql.append(" where u.login_name= :loginName ");
//		sql.append(getUserCondition());
//
//		// 定义返回的bean类型
//		// 定义返回值类型
//		BeanPropertyRowMapper<UserInfo> mapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);
//
//		// 定义SQL 绑定参数值
//		SqlParameterSource param = new BeanPropertySqlParameterSource(query);
//
//		// 不能使用 queryForObject ，如果查询到 0 条记录会抛异常
//		// org.springframework.dao.EmptyResultDataAccessException
//		List<UserInfo> list = getQueryDao().query(sql.toString(),mapper, param);
//		UserInfo user = null;
//		if (null != list && list.size() > 0) {
//			user = list.get(0);
//		}
//
//		return user;

	}

	public UserInfo updateUserInfo(UserInfo query){
		return query;
	}

}
